﻿using Aliyun.OSS;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace store.cyt369.com.utility
{
    public class SystemSettingService
    {
        #region init
        private static readonly SystemSettingService m_Instance = new SystemSettingService();
        private SystemSettingService()
        {
#if DEBUG
            string basePath = Directory.GetCurrentDirectory();
            if (!File.Exists(Path.Combine(basePath, "appsettings.json")))
            {
                basePath = new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location).DirectoryName;
            }
#else
            string basePath = new System.IO.FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location).DirectoryName;
#endif
            var builder = new ConfigurationBuilder()
                .SetBasePath(basePath).AddJsonFile(@"appsettings.json", optional: true, reloadOnChange: true);

            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; set; }

        /// <summary>
        /// 类型实例
        /// </summary>
        public static SystemSettingService Instance
        {
            get
            {
                return m_Instance;
            }
        }
        #endregion

        #region Connection
        //private Connection m_UsersDB;
        //public Connection UsersDB
        //{
        //    get
        //    {
        //        if (m_UsersDB == null)
        //        {
        //            m_UsersDB = new Connection()
        //            {
        //                DBType = Configuration.GetSection("DBConnectionStrings:UsersDB:DBType").Value,
        //                DBSchema = Configuration.GetSection("DBConnectionStrings:UsersDB:Schema").Value,
        //                ConnectionString = Configuration.GetSection("DBConnectionStrings:UsersDB:ConnectionString").Value
        //            };
        //        }

        //        return m_UsersDB;
        //    }
        //}

        // 这里是apsetting.json的相关配置信息
        private Connection m_StoreDB;
        public Connection StoreDB
        {
            get
            {
                if (m_StoreDB == null)
                {
                    // 这里从配置文件中取得相关的信息
                    m_StoreDB = new Connection()
                    {
                        DBType = Configuration.GetSection("DBConnectionStrings:StoreDB:DBType").Value,
                        DBSchema = Configuration.GetSection("DBConnectionStrings:StoreDB:Schema").Value,
                        ConnectionString = Configuration.GetSection("DBConnectionStrings:StoreDB:ConnectionString").Value
                    };
                }

                return m_StoreDB;
            }
        }

        private Connection m_BaseDB;
        public Connection BaseDB
        {
            get
            {
                if (m_BaseDB == null)
                {
                    m_BaseDB = new Connection()
                    {
                        DBType = Configuration.GetSection("DBConnectionStrings:BaseDB:DBType").Value,
                        DBSchema = Configuration.GetSection("DBConnectionStrings:BaseDB:Schema").Value,
                        ConnectionString = Configuration.GetSection("DBConnectionStrings:BaseDB:ConnectionString").Value
                    };
                }

                return m_BaseDB;
            }
        }

        private Connection m_RedisDB;
        public Connection RedisDB
        {
            get
            {
                if (m_RedisDB == null)
                {
                    m_RedisDB = new Connection()
                    {
                        DBType = Configuration.GetSection("DBConnectionStrings:RedisDB:DBType").Value,
                        DBSchema = Configuration.GetSection("DBConnectionStrings:RedisDB:Schema").Value,
                        ConnectionString = Configuration.GetSection("DBConnectionStrings:RedisDB:ConnectionString").Value
                    };
                }

                return m_RedisDB;
            }
        }
        #endregion

        #region SystemSetting

        public string SystemName
        {
            get
            {
                return Configuration.GetSection("SystemSetting:SystemName").Value;
            }
        }

        public int PageSize
        {
            get
            {
                int id = 0;
                int.TryParse(Configuration.GetSection("SystemSetting:PageSize").Value, out id);
                if (id <= 0)
                {
                    id = 20;
                }
                return id;
            }
        }

        /// <summary>
        /// 登录超时时间
        /// </summary>
        public int IdleTimeout
        {
            get
            {
                int n = 0;
                int.TryParse(Configuration.GetSection("SystemSetting:IdleTimeout").Value, out n);
                if (n <= 0)
                {
                    n = 120;
                }
                return n;
            }
        }

        /// <summary>
        /// 验证码超时时间
        /// </summary>
        public int CaptchaTimeout
        {
            get
            {
                int n = 0;
                int.TryParse(Configuration.GetSection("SystemSetting:CaptchaTimeout").Value, out n);
                if (n <= 0)
                {
                    n = 120;
                }
                return n;
            }
        }


        public string DesKV
        {
            get
            {
                return Configuration.GetSection("SystemSetting:DesKV").Value;
            }
        }
        public string DesIV
        {
            get
            {
                return Configuration.GetSection("SystemSetting:DesIV").Value;
            }
        }
        public string LoginDefaultRedirect
        {
            get
            {
                return Configuration.GetSection("SystemSetting:LoginDefaultRedirect").Value;
            }
        }
        public string QQCallback
        {
            get
            {
                return Configuration.GetSection("SystemSetting:QQCallback").Value;
            }
        }
        public string AppID
        {
            get
            {
                return Configuration.GetSection("SystemSetting:AppID").Value;
            }
        }
        public string AppKey
        {
            get
            {
                return Configuration.GetSection("SystemSetting:AppKey").Value;
            }
        }
        public string UserServiceURL
        {
            get
            {
                return Configuration.GetSection("SystemSetting:UserServiceURL").Value;
            }
        }
        public string UserCenterRegisterURL
        {
            get
            {
                return Configuration.GetSection("SystemSetting:UserCenterRegisterURL").Value;
            }
        }
        public string UserCenterLoginURL
        {
            get
            {
                return Configuration.GetSection("SystemSetting:UserCenterLoginURL").Value;
            }
        }
        public string UserCenterLogOffURL
        {
            get
            {
                return Configuration.GetSection("SystemSetting:UserCenterLogOffURL").Value;
            }
        }
        public string LoginReturnURL
        {
            get
            {
                return Configuration.GetSection("SystemSetting:LoginReturnURL").Value;
            }
        }
        #endregion

        #region path
        public string LoginPath
        {
            get
            {
                var path = Configuration.GetSection("AppPath:LoginPath").Value;
                return path;
            }
        }

        #endregion


        #region Aliyun.OSS
        private store.cyt369.com.utility.Aliyuns.EndpointParameter m_StoreInternalBucket;
        /// <summary>
        /// 内容管理系统图片/文件库
        /// </summary>
        public store.cyt369.com.utility.Aliyuns.EndpointParameter StoreInternalBucket
        {
            get
            {
                if (m_StoreInternalBucket == null)
                {
                    m_StoreInternalBucket = new store.cyt369.com.utility.Aliyuns.EndpointParameter()
                    {
                        Endpoint = Configuration.GetSection("Aliyun.OSS:Store:Internal").Value,
                        AccessKeyId = Configuration.GetSection("Aliyun.OSS:Store:AccessKeyId").Value,
                        AccessKeySecret = Configuration.GetSection("Aliyun.OSS:Store:AccessKeySecret").Value,
                        Bucket = Configuration.GetSection("Aliyun.OSS:Store:Public.Bucket").Value,
                        HttpURL = Configuration.GetSection("Aliyun.OSS:Store:Public.Bucket.HTTP").Value
                    };
                }

                return m_StoreInternalBucket;
            }
        }
        #endregion
    }
}
